package com.mensanyun.yminder

import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.layout.positionInRoot
import androidx.compose.ui.window.MenuBar
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import com.mensanyun.yminder.core.connector.DefaultConnector
import com.mensanyun.yminder.core.data.Sheet
import com.mensanyun.yminder.core.data.Topic
import com.mensanyun.yminder.core.layout.ContentLayoutConstrains
import com.mensanyun.yminder.core.layout.DefaultLayout

@Composable
@Preview
fun App() {
    val sheet = mockSheet()
    var tabIndex by remember { mutableStateOf(0) }

    MaterialTheme {
        Scaffold(modifier = Modifier.background(Color.LightGray), topBar = {
            Button(onClick = {}) {
                Text("New")
            }

        }) {
            Box(modifier = Modifier.background(Color.White).onGloballyPositioned {
                ContentLayoutConstrains.contentAreaOffset = it.positionInRoot()
            }) {
                DefaultLayout().layout(sheet)
                Canvas(modifier = Modifier.fillMaxSize()) {
                    DefaultConnector.connect(sheet.primaryTopic, this)
                }
            }
        }
//
//        Column {
//            TabRow(selectedTabIndex = tabIndex, modifier = Modifier.background(Color.LightGray)) {
//                Tab(
//                    selected = tabIndex == 0,
//                    onClick = { tabIndex = 0 },
//                    text = { Text("New") },
//                    modifier = Modifier.background(Color.LightGray)
//                )
//                Tab(selected = tabIndex == 1, onClick = { tabIndex = 1 }, text = { Text("111") })
//            }
//
//        }


    }
}

private fun mockSheet(): Sheet {
    val sheet = Sheet("123", "Sheet1")
    val primaryTopic = Topic("111", "Primary Topic")
    val topic1 = Topic("112", "Topic11")
    val topic2 = Topic("113", "Topic12")
    val topic3 = Topic("114", "Topic13")


    val topic11 = Topic("1121", "Topic111")
    val topic12 = Topic("113", "Topic112")

    val topic21 = Topic("1131", "Topic121")
    val topic22 = Topic("1132", "Topic122")


    topic1.addSubTopic(topic11)
    topic1.addSubTopic(topic12)

    topic2.addSubTopic(topic21)
    topic2.addSubTopic(topic22)

    primaryTopic.addSubTopic(topic1)
    primaryTopic.addSubTopic(topic2)
    primaryTopic.addSubTopic(topic3)

    sheet.primaryTopic = primaryTopic
    return sheet
}


fun main() = application {
    Window(onCloseRequest = ::exitApplication) {
        MenuBar {
            Menu("File") {
                Item("New", onClick = { println("New") })
                Item("Open", onClick = { println("Open") })
            }
        }

        App()
//        testText()
    }
}

@Composable
fun testText() {
    MaterialTheme {
        Box {
            DefaultLayout().layout(mockSheet())
        }
    }
}
